package com.ruoyi.service.jpjs.capital.impl;

import com.ruoyi.mapper.jpjs.capital.CapitalInputMapper;
import com.ruoyi.service.jpjs.capital.CapitalInputService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zeo
 * @explain 劳资数据录入
 * @date 2021/4/10 11:14
 */
@Service
public class CapitalInputServiceImpl implements CapitalInputService {

    @Autowired
    CapitalInputMapper capitalInputMapper;

    @Override
    public List<Map<String, Object>> selectAllPro(Map map) {
        return capitalInputMapper.selectAllPro(map);
    }

    @Override
    public List<Map<String, Object>> selectDataDate(String proId) {
        return capitalInputMapper.selectDataDate(proId);
    }

    @Override
    public List<Map<String, Object>> selectPositionByProjectId(String projectId) {
        return capitalInputMapper.selectPositionByProjectId(projectId);
    }

    @Override
    public List<Map<String, Object>> selectData(String project_id, String years, String months, String days) {
        String sql = "exec GetCapitalData '" + project_id + "'," + years + "," + months + "," + days;
        capitalInputMapper.selectData(sql);

        //拼接sql,找出该项目的所有的施工部位
        List<String> positionList = capitalInputMapper.selectPosition(project_id);
        StringBuffer getDataSql = new StringBuffer("select work_name 工种,max(create_date)create_date,max(work_id)work_id,max(years) years,max(months) months,max(days) days,");
        for (String position : positionList) {
            getDataSql.append("isnull(max(case position_name when '");
            getDataSql.append(position);
            getDataSql.append("' then person_num else 0 end),0) as ");
            getDataSql.append("'");
            getDataSql.append(position);
            getDataSql.append("',");
        }
        getDataSql.deleteCharAt(getDataSql.length() - 1);
        getDataSql.append(" from capital_data_temporary group by work_name order by create_date");
        List<Map<String, Object>> dataList = capitalInputMapper.selectData(getDataSql.toString());
        return dataList;
    }

    @Override
    public int updateCapitalData(String years, String months, String days, String work_id, String position, String value) {
        //用工种ID寻找项目ID
        String projectId = capitalInputMapper.selectProjectIdByWorkId(work_id);
        //再拿施工部位名称查询它的ID
        String positionId = capitalInputMapper.selectPositionIdByPositionName(position, projectId);
        //修改
        Map map = new HashMap();
        map.put("years", years);
        map.put("months", months);
        map.put("days", days);
        map.put("work_id", work_id);
        map.put("positionId", positionId);
        map.put("projectId", projectId);
        map.put("value", value);
        if (capitalInputMapper.countCapitalData(map) == 0) {
            return capitalInputMapper.insertCapitalData(map);
        } else {
            return capitalInputMapper.updateCapitalData(map);
        }
    }

    @Override
    public Map selectCapitalDataById(String id) {
        return capitalInputMapper.selectCapitalDataById(id);
    }

    @Override
    public int proDataAdd(Map map) {
        return capitalInputMapper.proDataAdd(map);
    }

    @Override
    public int deleteData(Map map) {
        return capitalInputMapper.deleteData(map) + capitalInputMapper.deleteDataTemporary(map);
    }
}
